/**
 * # # THIS FILE IS PART OF drug-server PROJECT
 * # #
 * # MybatisPlusConfig.java - MybatisPlusConfig
 * #
 * # THIS PROGRAM IS OPENSOURCE SOFTWARE, IS LICENSED UNDER BSD
 * # YOU SHOULD HAVE RECEIVED A COPY OF WTFPL LICENSE
 * #
 * # Copyright (c) 2018 Ting Fan
 */
package com.cloudkinto.config;

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import com.baomidou.mybatisplus.extension.plugins.OptimisticLockerInterceptor;
import com.baomidou.mybatisplus.extension.plugins.PaginationInterceptor;
import com.cloudkinto.base.BaseController;
import com.cloudkinto.common.annotations.UserSessionRequired;
import com.cloudkinto.common.constant.SysConstant;
import org.apache.ibatis.reflection.MetaObject;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.transaction.annotation.EnableTransactionManagement;

import java.util.Date;

@EnableTransactionManagement
@Configuration
@MapperScan("com.cloudkinto.dao")
public class MybatisPlusConfig extends BaseController implements MetaObjectHandler {
    /**
     * 分页插件
     */
    @Bean
    public PaginationInterceptor paginationInterceptor() {
        PaginationInterceptor paginationInterceptor = new PaginationInterceptor();
        // paginationInterceptor.setLimit(你的最大单页限制数量，默认 500 条，小于 0 如 -1 不受限制);
        return paginationInterceptor;
    }

    @Override
    @UserSessionRequired
    public void insertFill(MetaObject metaObject) {
        // 先判断是否存在该字段
        if (metaObject.hasSetter("createDate")) {
            //为空字段填充数据
            if (getFieldValByName("createDate", metaObject) == null) {
                this.setInsertFieldValByName("createDate", new Date(), metaObject);
            }
        }
        if (metaObject.hasSetter("createTime")) {
            if (getFieldValByName("createTime", metaObject) == null) {
                this.setInsertFieldValByName("createTime", new Date(), metaObject);
            }
        }
        if (metaObject.hasSetter("deleteFlag")) {
            if (getFieldValByName("deleteFlag", metaObject) == null) {
                this.setInsertFieldValByName("deleteFlag", SysConstant.DELETE_FLAG_FALSE, metaObject);
            }
        }
        if (metaObject.hasSetter("createBy")) {
            if (getFieldValByName("createBy", metaObject) == null) {
                this.setInsertFieldValByName("createBy", getCurrentUserIdNoEx(), metaObject);
            }
        }
        if (metaObject.hasSetter("createByName")) {
            if (getFieldValByName("createByName", metaObject) == null) {
                this.setInsertFieldValByName("createByName", getUserName(), metaObject);
            }
        }
        if (metaObject.hasSetter("updateBy")) {
            if (getFieldValByName("updateBy", metaObject) == null) {
                this.setInsertFieldValByName("updateBy", getCurrentUserIdNoEx(), metaObject);
            }
        }
        if (metaObject.hasSetter("updateByName")) {
            if (getFieldValByName("updateByName", metaObject) == null) {
                this.setInsertFieldValByName("updateByName", getUserName(), metaObject);
            }
        }
        if (metaObject.hasSetter("tenantId")) {
            if (getFieldValByName("tenantId", metaObject) == null) {
                this.setInsertFieldValByName("tenantId", getTenantId(), metaObject);
            }
        }
        if (metaObject.hasSetter("whId")) {
            if (getFieldValByName("whId", metaObject) == null) {
                this.setInsertFieldValByName("whId", getWarehouseId(), metaObject);
            }
        }
        if (metaObject.hasSetter("warehouseId")) {
            if (getFieldValByName("warehouseId", metaObject) == null) {
                this.setInsertFieldValByName("warehouseId", getWarehouseId(), metaObject);
            }
        }
        if (metaObject.hasSetter("companyId")) {
            if (getFieldValByName("companyId", metaObject) == null) {
                this.setInsertFieldValByName("companyId", getCompanyId(), metaObject);
            }
        }
    }

    @Override
    @UserSessionRequired
    public void updateFill(MetaObject metaObject) {
        if (metaObject.hasSetter("updateDate")) {
            //为空字段填充数据
            if (getFieldValByName("updateDate", metaObject) == null) {
                this.setUpdateFieldValByName("updateDate", new Date(), metaObject);
            }
        }
        if (metaObject.hasSetter("updateTime")) {
            if (getFieldValByName("updateTime", metaObject) == null) {
                this.setUpdateFieldValByName("updateTime", new Date(), metaObject);
            }
        }
        if (metaObject.hasSetter("updateBy")) {
            if (getFieldValByName("updateBy", metaObject) == null) {
                this.setUpdateFieldValByName("updateBy", getCurrentUserIdNoEx(), metaObject);
            }
        }
        if (metaObject.hasSetter("updateByName")) {
            if (getFieldValByName("updateByName", metaObject) == null) {
                this.setUpdateFieldValByName("updateByName", getUserName(), metaObject);
            }
        }
    }

    /**
     * 乐观锁
     */
    @Bean
    public OptimisticLockerInterceptor optimisticLockerInterceptor() {
        return new OptimisticLockerInterceptor();
    }
}
